﻿Imports ControllerSigeco

Public Class frmMercaderiaLote

    Private Sub frmMercaderiaListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgMercaderia.AutoGenerateColumns = False
        CargarCombos()
        CargarMercaderias()
    End Sub

    Private m_frmMercaderias As frmMercaderiaLoteAjuste
    Public Property frmMercaderias() As frmMercaderiaLoteAjuste
        Get
            Return m_frmMercaderias
        End Get
        Set(ByVal value As frmMercaderiaLoteAjuste)
            m_frmMercaderias = value
        End Set
    End Property

    Dim classController As New controller_stock

    Private _idMercaderia As Integer = 0

    Public Property idMercaderia() As Integer
        Get
            Return _idMercaderia
        End Get
        Set(ByVal value As Integer)
            _idMercaderia = value
        End Set
    End Property

    Private _idMercaderiaLote As Integer = 0

    Public Property idMercaderiaLote() As Integer
        Get
            Return _idMercaderiaLote
        End Get
        Set(ByVal value As Integer)
            _idMercaderiaLote = value
        End Set
    End Property

    Private Sub CargarMercaderias()
        Try
            idMercaderia = 0
            idMercaderiaLote = 0
            txtMercaderia.Focus()

            btnBuscar.Enabled = True

            Dim idMarca As Integer = 0
            If cbxMarca.Checked Then
                idMarca = cboMarca.SelectedValue
            End If

            Dim idDeposito As Integer = 0
            If cbxDeposito.Checked Then
                idDeposito = cboDeposito.SelectedValue
            End If

            Dim idRubro As Integer = 0
            If cbxRubro.Checked Then
                idRubro = cboRubro.SelectedValue
            End If

            Dim ds As New DataTable
            ds = classController.get_mercaderias_All(oiml.dbConn, 0, txtMercaderia.Text, idMarca, idRubro, True, txtCodigoBarra.Text, idDeposito, 0)

            If Not ds Is Nothing Then
                dgMercaderia.DataSource = ds
            Else
                dgMercaderia.DataSource = Nothing
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub CargarCombos()
        Dim dsMarca As New DataTable
        dsMarca = classController.get_marcas(oiml.dbConn, 0, "")
        If Not dsMarca Is Nothing Then
            cboMarca.DataSource = dsMarca
            cboMarca.DisplayMember = "marca"
            cboMarca.ValueMember = "idMarca"
        End If

        Dim dsRubro As New DataTable
        dsRubro = classController.get_rubros(oiml.dbConn, 0, "")
        If Not dsRubro Is Nothing Then
            cboRubro.DataSource = dsRubro
            cboRubro.DisplayMember = "rubro"
            cboRubro.ValueMember = "idRubro"
        End If

        Dim dsDepo As New DataTable
        dsDepo = classController.get_depositos(oiml.dbConn, 0, "", "", True)
        If Not dsDepo Is Nothing Then
            cboDeposito.DataSource = dsDepo
            cboDeposito.DisplayMember = "descripcion"
            cboDeposito.ValueMember = "idDeposito"
        End If

    End Sub

    Private Sub editar()
        Try

            If dgMercaderia.SelectedRows.Count > 0 Then

                idMercaderiaLote = dgMercaderia.SelectedRows(0).Cells(1).Value
                If idMercaderiaLote > 0 Then
                    frmMercaderias = New frmMercaderiaLoteAjuste()
                    frmMercaderias.idMercaderiaLote = idMercaderiaLote
                    frmMercaderias.soloLectura = True
                    frmMercaderias.ShowDialog()
                    CargarMercaderias()
                End If

            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        CargarMercaderias()

    End Sub

    Private Sub dgMercaderia_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgMercaderia.KeyDown
        If e.KeyCode = Keys.Enter Then
            editar()
        End If
    End Sub

    Private Sub dgMercaderia_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgMercaderia.DoubleClick
        editar()
    End Sub

    Private Sub txtMarca_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMercaderia.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMercaderias()
        End If
    End Sub

    Private Sub dgMercaderia_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgMercaderia.CellClick
        Try

            If dgMercaderia.SelectedRows.Count > 0 Then
                idMercaderia = dgMercaderia.SelectedRows(0).Cells(0).Value
                idMercaderiaLote = dgMercaderia.SelectedRows(0).Cells(1).Value
            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnVer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVer.Click
          editar()
    End Sub

    Private Sub btnDarBaja_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDarBaja.Click
        If idMercaderiaLote > 0 Then
            If MessageBox.Show("Desea dar de baja el registro seleccionado?", "Mercaderias", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Baja_mercaderia(oiml.dbConn, idMercaderiaLote, oiml.idUsuario) Then
                    txtMercaderia.Text = ""
                    CargarMercaderias()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Mercaderias", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

    Private m_frmReporte As frmReportes
    Public Property frmRpts() As frmReportes
        Get
            Return m_frmReporte
        End Get
        Set(ByVal value As frmReportes)
            m_frmReporte = value
        End Set
    End Property

    Private Sub btnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimir.Click
        If frmRpts Is Nothing Then
            frmRpts = New frmReportes
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "LOTES"
            frmRpts.datos_Reporte = CType(dgMercaderia.DataSource, DataTable).Copy
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New frmReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "LOTES"
                frmRpts.datos_Reporte = CType(dgMercaderia.DataSource, DataTable).Copy
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "LOTES"
                frmRpts.datos_Reporte = CType(dgMercaderia.DataSource, DataTable).Copy
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If frmRpts Is Nothing Then
            frmRpts = New frmReportes
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "STOCKMINIMO"
            frmRpts.datos_Reporte = classController.get_stock_minimo(oiml.dbConn)
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New frmReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "STOCKMINIMO"
                frmRpts.datos_Reporte = classController.get_stock_minimo(oiml.dbConn)
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "STOCKMINIMO"
                frmRpts.datos_Reporte = classController.get_stock_minimo(oiml.dbConn)
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If frmRpts Is Nothing Then
            frmRpts = New frmReportes
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "COSTOMERCADERIA"
            frmRpts.datos_Reporte = classController.get_costo_mercaderia(oiml.dbConn)
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New frmReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "COSTOMERCADERIA"
                frmRpts.datos_Reporte = classController.get_costo_mercaderia(oiml.dbConn)
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "COSTOMERCADERIA"
                frmRpts.datos_Reporte = classController.get_costo_mercaderia(oiml.dbConn)
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If frmRpts Is Nothing Then
            frmRpts = New frmReportes
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "MERCADERIASRANKING"
            frmRpts.datos_Reporte = classController.get_mercaderia_ranking(oiml.dbConn)
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New frmReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "MERCADERIASRANKING"
                frmRpts.datos_Reporte = classController.get_mercaderia_ranking(oiml.dbConn)
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "MERCADERIASRANKING"
                frmRpts.datos_Reporte = classController.get_mercaderia_ranking(oiml.dbConn)
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If
    End Sub

End Class